# -*- coding: utf-8 -*-

from openerp.osv import osv


class stock_picking(osv.osv):
    _inherit = 'stock.picking'

    def do_picking_done(self, cr, uid, ids, context=None):
        context = dict(context) or {}
        context.update({'option': 'assigned'})
        for picking_id in ids:
            # 调用系统入库按钮“移动”对应的方法do_enter_transfer_details，完成入库操作
            self.pool.get("stock.picking").do_enter_transfer_details(cr, uid, [picking_id], context=context)
            transfer_details_ids = self.pool.get('stock.transfer_details').search(cr, uid, [('picking_id', '=', picking_id)], context=context)
            self.pool.get('stock.transfer_details').do_detailed_transfer(cr, uid, transfer_details_ids, context=context)

    def get_pickings_by_search(self, cr, uid, search_type, search_word, context=None):
        domain = [
            # ('delivery_state', '=', 'waiting_delivery'),  # 仅仅需要列出待送货的单据？
            # ('state', 'in', ('assigned', 'done')),
            # ('picking_type_code', '=', 'outgoing'),
            # ('delivery_user_id', '=', False),   # 要限制被其他人员送货的单据嘛？
        ]

        if search_type == 'all':
            domain.extend(['|', ('partner_id.name', 'ilike', search_word), ('name', 'ilike', search_word)])
        elif search_type == 'partner':
            domain.append(('partner_id.name', 'ilike', search_word))
        elif search_type == 'picking':
            domain.append(('name', 'ilike', search_word))

        picking_ids = self.search(cr, uid, domain, context=context)

        res = []
        for picking in self.browse(cr, uid, picking_ids, context=context):
            res.append({
                    'id': picking.id,
                    'name': picking.name,
                    'delivery_user': picking.delivery_user_id.name or '',
                    'partner': picking.partner_id.name,
                    'address': self._get_address_by_order(picking.so_id),
                    'state': picking.state,
                    'delivery_state': picking.delivery_state,
                    'is_display_detail': False,
                    'choose': False,
                    'hidden': False,
                    'lines': [],
                })

        return res

    def get_pickings_by_user(self, cr, uid, user_id, context=None):
        picking_ids = self.search(cr, uid, [('delivery_user_id', '=', int(user_id)),
            ('delivery_state', '=', 'shipping'), ('state', '=', 'done'), ('picking_type_code', '=', 'outgoing')], context=context)
        res = []
        for picking in self.browse(cr, uid, picking_ids, context=context):
            res.append({
                    'id': picking.id,
                    'name': picking.name,
                    'delivery_user': picking.delivery_user_id.name or '',
                    'partner': picking.partner_id.name,
                    'address': self._get_address_by_order(picking.so_id),
                    'state': picking.state,
                    'delivery_state': picking.delivery_state,
                    'is_display_detail': False,
                    'choose': False,
                    'hidden': False,
                    'lines': [],
                })

        return res

    def get_picking_by_show(self, cr, uid, id, context=None):
        picking = self.browse(cr, uid, int(id), context=context)

        return {
            'name': picking.name,
            'delivery_user': picking.delivery_user_id.name or '',
            'address': self._get_address_by_order(picking.so_id),
            'state': picking.state,
            'delivery_state': picking.delivery_state,
        }

    def get_picking_message_by_show(self, cr, uid, id, user_id, context=None):
        picking = self.browse(cr, uid, int(id), context=context)
        user_id = int(user_id)

        message = '<ul>'
        if picking.state != 'assigned':
            message += '<li>%s</li>' % u'出库单的状态不是待确认移动'
        if picking.delivery_state == 'delivery_done':
            message += '<li>%s</li>' % u'出库单已经被送过货了'
        if picking.delivery_user_id and picking.delivery_user_id.id != user_id:
            message += '<li>%s</li>' % u'出库单正在被其他送货人员配送中'
        if picking.picking_type_id and picking.picking_type_id.code != 'outgoing':
            message += '<li>%s</li>' % u'当前的单据不是出库单'
        if picking.state == 'done' and picking.picking_type_id and picking.picking_type_id.code == 'outgoing' and picking.delivery_state != 'done' and picking.delivery_user_id.id != user_id:
            message = '<ul><li>%s</li>' % u'当前的出库单已经被移动了，但是并没有被送货'
            message += "</ul><p ms-visible='!need_change'>%s</p>" % u'更改送货人员成功，请开始送货'
            message += "<a href='#' class='delivery-picking-detail-button' ms-click='change_user' ms-visible='need_change'>更改送货人员</a>"
        elif picking.state == 'done' and picking.picking_type_id and picking.picking_type_id.code == 'outgoing' and picking.delivery_state != 'delivery_done' and picking.delivery_user_id.id == user_id:
            message = "<p>%s</p>" % u'当前出库单已经处于送货状态'
            return message
        else:
            message += '</ul>'

        return '<div>扫码出库失败，以下是可能的原因</div>' + message
        # return message + '</ul>'
